package com.smart.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.pojo.ResultBean;
import com.smart.pojo.Role;
import com.smart.req.RoleReq;
import com.smart.resp.RoleResp;
import com.smart.service.IRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lyj
 * @since 2023-04-21
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    /**
     * 请求角色信息，根据条件进行分页查询
     *
     * @param roleReq
     * @return
     */
    @GetMapping("queryPageByCondition")
    public ResultBean queryPageByCondition(RoleReq roleReq) {
        System.out.println("请求角色分页的条件：" + roleReq);
        Page<RoleResp> rolePage = new Page<>(roleReq.getPageNum(), roleReq.getPageSize());
        roleService.queryPageByCondition(roleReq, rolePage);
        return ResultBean.success(rolePage);
    }

    /**
     * 新增或修改角色信息
     */
    @PostMapping("saveOrUpdate")
    @RequiresPermissions({"role:save"})
    public ResultBean saveOrUpdate(@RequestBody Role role) {
        System.out.println("新增或修改的角色信息：" + role);
        boolean rs = roleService.saveOrUpdate(role);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 逻辑删除角色信息
     */
    @RequiresPermissions({"role:del"})
    @DeleteMapping("deleteById/{id}")
    public ResultBean deleteById(@PathVariable("id") Long id) {
        System.out.println("要删除角色的Id:" + id);
        boolean rs = roleService.deleteById(id);
        return rs ? ResultBean.success() : ResultBean.error();
    }
}
